define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('Inject02Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog','$calculater', "$gallery",
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog,$calculater, $gallery) {
            /**
             * options = {
             *     machineItem: {}, 從 B0201、B0214、B0215 傳來的
             *     plotItem: {}    從 B0201、B0214、B0215 傳來的
             * }
            **/
            this.init = function (machine, plot) {

                var openDialog = function(data){
                    $mdDialog.dialog(
                        'module/moduleInject/program/Inject02/Inject02.html',
                        function (dialog) {
                            return {
                                css_url: 'module/moduleInject/program/Inject02/Inject02.css',
                                op_no: plot.op_no,
                                plot_no: plot.plot_no,
                                machine_no: machine.machine_no,
                                machine_name: machine.machine_name,
                                product_no: plot.item_no,
                                product_name: plot.item_name,
                                program_no: data.program_no_list[0].program_no,
                                program_no_list: data.program_no_list,
                                version_no: data.version_no,
                                recipe_no: data.recipe_no,
                                production_phase_list: data.production_phase_list,
                                now_item_title: data.production_phase_list[0].title,
                                now_item_recipe_list: data.production_phase_list[0].recipe_list,
                                description: data.description,
                                btn_check: data.eqx_compare,
                                back: function(){
                                    dialog.hide();
                                },
                                changeProgramNo: function(){
                                    $rootScope.showQueryList({
                                        key:'program_no',
                                        header : [
                                            {label: $filter('translate')('Inject02.queryList.program_no'), code:'program_no'},
                                            {label: $filter('translate')('Inject02.queryList.version_no'), code:'version_no'},
                                            //{label: $filter('translate')('Inject02.queryList.recipe_no'), code:'recipe_no'}
                                            { label: $filter('translate')('Inject02.queryList.description'), code: 'description' }
                                        ],
                                        query: function(conditions, feedback){
                                            feedback(dialog.program_no_list);
                                        },
                                        selectCode: dialog.program_no,
                                        confirm: function(selectItem, dialogType){
                                            dialog.program_no = selectItem.program_no;
                                            dialog.version_no = selectItem.version_no;
                                            dialog.recipe_no = selectItem.recipe_no;
                                            dialog.description = selectItem.description;
                                            dialogType.hide();
                                            var content = {
                                                machine_no: dialog.machine_no,
                                                lot_no: dialog.plot_no,
                                                op_no: dialog.op_no,
                                                program_no: dialog.program_no,
                                                pro_ver: dialog.product_no,
                                                recipe_no: dialog.recipe_no,
                                                description: dialog.description
                                            };
                                            productionPhaseRecipeInfoGet(content, function(data){
                                                dialog.production_phase_list = data.production_phase_list;
                                                for(var i = 0; i < data.production_phase_list.length; i++){
                                                    if(data.production_phase_list[i].title == dialog.now_item_title){
                                                        dialog.now_item_recipe_list = data.production_phase_list[i].recipe_list;
                                                        break;
                                                    }
                                                };
                                            });
                                        }
                                    });
                                },
                                changeProductionPhase: function(item){
                                    dialog.now_item_title = item.title;
                                    dialog.now_item_recipe_list = item.recipe_list;
                                },
                                bindingRecipe: function(){
                                    var content = {
                                        machine_no: dialog.machine_no,
                                        lot_no: dialog.plot_no,
                                        op_no: dialog.op_no,
                                        recipe_no: dialog.recipe_no,
                                    };
                                    bindingRecipeProcess(content, function(data){
                                        $rootScope.showToast($filter('translate')('Inject02.msg.binding_success'));
                                    });
                                },
                                compareMachineState: function(){
                                    var content = {
                                        machine_no: dialog.machine_no,
                                        lot_no: dialog.plot_no,
                                        op_no: dialog.op_no,
                                        program_no: dialog.program_no,
                                        recipe_no: dialog.recipe_no
                                    };
                                    // 20210915 modify by John for M#85102: 車間參數設定中的EQX_URL有值才往後進行
                                    if (data.eqx_compare == true) {
                                        compareMachineStateInfoGet(content, function (data) {
                                            dialog.production_phase_list = data.production_phase_list;
                                            dialog.now_item_recipe_list = data.production_phase_list[0].recipe_list;
                                        });
                                    }
                                }
                            };
                        }
                    );
                };

                productionPhaseRecipeInfoGet({
                    machine_no: machine.machine_no,
                    lot_no: plot.plot_no,
                    op_no: plot.op_no,
                    program_no: '', // 沒傳 program_no，後台預帶第一筆資料
                    pro_ver: plot.item_no,
                    recipe_no: ''
                }, function(data){
                    openDialog(data);
                });

                // 綁定 Recipe參數
                function bindingRecipeProcess(content, callback){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_INJECT.injection_recipe_bind_lot_process',
                        content : content,
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                };

                // 取得 Recipe參數資料
                function productionPhaseRecipeInfoGet(content, callback){
                    // var data = {
                    //     program_no_list: [
                    //         {
                    //             program_no: 'ITM01',
                    //             version_no: '01',
                    //             recipe_no: '001'
                    //         },
                    //         {
                    //             program_no: 'ITM02',
                    //             version_no: '02',
                    //             recipe_no: '002'
                    //         },
                    //         {
                    //             program_no: 'ITM03',
                    //             version_no: '03',
                    //             recipe_no: '003'
                    //         },
                    //         {
                    //             program_no: 'ITM04',
                    //             version_no: '04',
                    //             recipe_no: '004'
                    //         },
                    //         {
                    //             program_no: 'ITM05',
                    //             version_no: '05',
                    //             recipe_no: '005'
                    //         }
                    //     ],
                    //     version_no: '001',
                    //     recipe_no: '001',
                    //     production_phase_list: [
                    //         {
                    //             title:'料管',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度1',
                    //                     none: '',
                    //                     recipe_segment_qty_1:50,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:40,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:30,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:20,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:10,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'關模',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度2',
                    //                     none: '',
                    //                     recipe_segment_qty_1:60,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:50,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:40,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:30,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:20,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'開模',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度3',
                    //                     none:'',
                    //                     recipe_segment_qty_1:70,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:60,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:50,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:40,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:30,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'射出',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度4',
                    //                     none:'',
                    //                     recipe_segment_qty_1:80,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:70,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:60,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:50,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:40,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'保壓',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:90,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:80,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:70,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:60,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:50,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'儲料',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:100,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:90,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:80,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:70,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:60,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'頂針',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:110,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:100,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:90,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:80,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:70,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'自定義',
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:120,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:110,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:100,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:90,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:80,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         }
                    //     ]
                    // };
                    // if(callback)
                    //     callback(data);
                    
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_INJECT.injection_recipe_info_get',
                        content : content,
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                };
                
                // 取得比對設備現況資料
                function compareMachineStateInfoGet(content, callback){
                    // var data = {
                    //     production_phase_list:[
                    //         {
                    //             title:'料管',
                    //             change_value:2,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度1',
                    //                     none:'',
                    //                     recipe_segment_qty_1:50,
                    //                     change_segment_qty_1:40,
                    //                     recipe_segment_qty_2:40,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:30,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:20,
                    //                     change_segment_qty_4:10,
                    //                     recipe_segment_qty_5:10,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'關模',
                    //             change_value:3,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度2',
                    //                     none:'',
                    //                     recipe_segment_qty_1:60,
                    //                     change_segment_qty_1:50,
                    //                     recipe_segment_qty_2:50,
                    //                     change_segment_qty_2:40,
                    //                     recipe_segment_qty_3:40,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:30,
                    //                     change_segment_qty_4:20,
                    //                     recipe_segment_qty_5:20,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'開模',
                    //             change_value:1,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度3',
                    //                     none:'',
                    //                     recipe_segment_qty_1:70,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:60,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:50,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:40,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:30,
                    //                     change_segment_qty_5:20,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'射出',
                    //             change_value:2,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度4',
                    //                     none:'',
                    //                     recipe_segment_qty_1:80,
                    //                     change_segment_qty_1:70,
                    //                     recipe_segment_qty_2:70,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:60,
                    //                     change_segment_qty_3:50,
                    //                     recipe_segment_qty_4:50,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:40,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'保壓',
                    //             change_value:3,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:90,
                    //                     change_segment_qty_1:80,
                    //                     recipe_segment_qty_2:80,
                    //                     change_segment_qty_2:70,
                    //                     recipe_segment_qty_3:70,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:60,
                    //                     change_segment_qty_4:50,
                    //                     recipe_segment_qty_5:50,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'儲料',
                    //             change_value:1,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:100,
                    //                     change_segment_qty_1:90,
                    //                     recipe_segment_qty_2:90,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:80,
                    //                     change_segment_qty_3:0,
                    //                     recipe_segment_qty_4:70,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:60,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'頂針',
                    //             change_value:2,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:110,
                    //                     change_segment_qty_1:0,
                    //                     recipe_segment_qty_2:100,
                    //                     change_segment_qty_2:0,
                    //                     recipe_segment_qty_3:90,
                    //                     change_segment_qty_3:80,
                    //                     recipe_segment_qty_4:80,
                    //                     change_segment_qty_4:70,
                    //                     recipe_segment_qty_5:70,
                    //                     change_segment_qty_5:0,
                    //                 }
                    //             ]
                    //         },
                    //         {
                    //             title:'自定義',
                    //             change_value:4,
                    //             recipe_list:[
                    //                 {
                    //                     recipe_name:'溫度5',
                    //                     none:'',
                    //                     recipe_segment_qty_1:120,
                    //                     change_segment_qty_1:110,
                    //                     recipe_segment_qty_2:110,
                    //                     change_segment_qty_2:100,
                    //                     recipe_segment_qty_3:100,
                    //                     change_segment_qty_3:90,
                    //                     recipe_segment_qty_4:90,
                    //                     change_segment_qty_4:0,
                    //                     recipe_segment_qty_5:80,
                    //                     change_segment_qty_5:70,
                    //                 }
                    //             ]
                    //         }
                    //     ]
                    // };
                    // if(callback)
                    //     callback(data);

                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_INJECT.injection_recipe_compare_info_get',
                        content : content,
                        success : function(data){
                            if(callback)
                                callback(data);
                        }
                    });
                };
                
            
                
        
            };
        }]);
});
